package com.shanjupay.merchant.service;

import com.alibaba.fastjson.JSON;
import com.shanjupay.common.domain.BusinessException;
import com.shanjupay.common.domain.CommonErrorCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import springfox.documentation.spring.web.json.Json;

import java.util.HashMap;
import java.util.Map;

/**
 * TODO
 *
 * @Description
 * @Author zhangbing
 * @Date 2024/5/10 8:47
 **/
@Service
@Slf4j
public class SmsServiceImpl implements SmsService{

    @Value("${sms.url}")
    private String smsUrl;
    @Value("${sms.effectiveTime}")
    private String effectiveTime;

    @Autowired
    private RestTemplate restTemplate;
    @Override
    public String SendMsg(String phone) {

        //定义一个路径 验证码设置的时间是600秒
        String url=smsUrl+"/generate?name=sms&effectiveTime="+effectiveTime;
        //参数  手机号 mobile 格式  json格式
        //请求体
        Map<String,Object> body = new HashMap();
        body.put("mobile",phone);
        //请求头
        HttpHeaders httpHeaders = new HttpHeaders();
        //设置成json格式
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        //封装请求参数
        HttpEntity entity = new HttpEntity(body,httpHeaders);
        Map map=null;
        try {
            // restTemplate请求方式:post
            ResponseEntity<Map> exchange = restTemplate.exchange(url, HttpMethod.POST, entity, Map.class);
            //获取响应 转换成json格式
            log.info("调用短信微服务发送验证码:返回值:{}", JSON.toJSONString(exchange));
            map = exchange.getBody();
        }catch (Exception e){
            log.info(e.getMessage(),e);
            throw new RuntimeException("发送验证码错误！");
        }

        if(map==null || map.get("result")==null){
            throw new RuntimeException("发送验证码错误！");
        }

            //获取body中的数据
            Map resultMap =(Map) map.get("result");
           // System.out.println(value);
        return  resultMap.get("key").toString();
    }

    @Override
    public void checkVerifiyCode(String verifiykey, String verifiyCode) throws BusinessException {
        //获取key 获取验证码 进行校验
        String url=smsUrl+"/verify?name=sms&verificationCode="+verifiyCode+"&verificationKey="+verifiykey;
        Map map=null;
        try {
        //请求校验验证码
            ResponseEntity<Map> exchange = restTemplate.exchange(url, HttpMethod.POST, HttpEntity.EMPTY, Map.class);
            map = exchange.getBody();
           log.info("校验验证码，响应内容:{}", JSON.toJSONString(map));
       }catch (Exception e){
           log.info(e.getMessage(),e);
          // throw new RuntimeException("验证码错误！");
            throw new BusinessException(CommonErrorCode.E_100102);
       }
        if(map==null || map.get("result")==null || !(Boolean) map.get("result")){
           // throw new RuntimeException("验证码错误！");
            throw new BusinessException(CommonErrorCode.E_100102);
        }

    }
}
